/**
 * 
 */
package com.kylin.erp.oa.system.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.spring.IocInterceptor;
import com.jfinal.plugin.spring.Inject;
import com.kylin.erp.dbmodel.TMenuResources;
import com.kylin.erp.oa.system.service.SystemMenuService;
import com.kylin.frame.core.BaseController;

/**
 * @author huxiang
 * 
 */
@Before(IocInterceptor.class)
public class MenuController extends BaseController {

	@Inject.BY_TYPE
	private SystemMenuService systemMenuService;

	public void index() {

		this.render("index.html");
	}

	public void query() {

		String parentId = getPara("id") == null ? "0" : getPara("id");

		this.renderJson(TMenuResources.dao.paginateDataTables(
				getParaToInt("page").intValue(), getParaToInt("rp").intValue(),
				"select id_,name_,url_,order_",
				"from t_menu_resources where parent_id_ = ?", parentId));
	}

	public void tree() {

		this.renderJson(systemMenuService.getUserMenus(0).toString());
	}

	@Before(Tx.class)
	public void add() {

		new TMenuResources().set("name_", this.getPara("name"))
				.set("url_", this.getPara("url")).set("company_id_", "kylin")
				.set("parent_id_", this.getPara("pId")).set("order_", 1).save();
		this.renderJson("msg", "添加成功");
	}

	@Before(Tx.class)
	public void del() {
		String[] ids = this.getPara("ids").split(",");
		for (String id : ids) {
			new TMenuResources().deleteById(id);
		}
		this.renderJson("msg", "删除成功");
	}

	public void get() {

		setAttr("menu", TMenuResources.dao.findById(this.getPara("id")))
				.render("modifyMenu.html");
	}

	@Before(Tx.class)
	public void modify() {
		TMenuResources.dao.findById(this.getPara("id"))
				.set("name_", this.getPara("name"))
				.set("url_", this.getPara("url")).update();
		this.renderJson("msg", "修改成功");
	}

}
